package com.lj.controller;


import com.lj.pojo.*;
import com.lj.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/total")
public class LoginAction {
    @Autowired
    AdminService adminService;
    @Autowired
    ClientService clientService;
    @Autowired
    StaffService staffService;
    @Autowired
    ManagerService managerService;
    @Autowired
    PmanagerService pmanagerService;
    @RequestMapping("/login")
    public String login(String username, String password, String identity, HttpServletRequest request) {
        if(identity.equals("admin")){
            Admin admin=adminService.login(username,password);
            if(admin!=null){
                String name=admin.getUsername();
                request.getSession().setAttribute("name",name);
                return "admin/main";
            }
            else {
                request.setAttribute("errmsg","用户名或密码不正确");
               return "forward:/index.jsp";
            }
        }
        else if(identity.equals("client")){
            Clientlj clientlj= clientService.login(username,password);
            if(clientlj!=null){
                String name=clientlj.getClientName();
                String id=clientlj.getClientId();
                request.getSession().setAttribute("name",name);
                request.getSession().setAttribute("id",id);
                return "client/main";
            }
            else {
                request.setAttribute("errmsg","用户名或密码不正确");
                return "forward:/index.jsp";
            }
        }
        else if(identity.equals("staff")){
            Stafflj stafflj= staffService.login(username,password);
            if(stafflj!=null){
                String name=stafflj.getStaffName();
                String id=stafflj.getStaffId();
                request.getSession().setAttribute("id",id);
                request.getSession().setAttribute("name",name);
                return "staff/main";
            }
            else {
                request.setAttribute("errmsg","用户名或密码不正确");
                return "forward:/index.jsp";
            }
        }
        else if(identity.equals("manager")){
            Managerlj managerlj= managerService.login(username,password);
            if(managerlj!=null){
                Pmanager pmanager= pmanagerService.select(username);
                String departmentName=pmanager.getDepartmentName();
                String name=managerlj.getManagerName();
                String id=managerlj.getManagerId();
                request.getSession().setAttribute("id",id);
                request.getSession().setAttribute("name",name);
                request.getSession().setAttribute("departmentName",departmentName);
                return "manager/main";
            }
            else {
                request.setAttribute("errmsg","用户名或密码不正确");
                return "forward:/index.jsp";
            }

        }
        return "forward:/index.jsp";
    }
}
